package org.jrfe.sys;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * JRFE Settings
 * @author Kalvis Freimanis
 *
 */
public class JRFESettings {
	/** Display width */
	public int 		width = 	800;
	
	/** Display height */
	public int 		height = 	600;
	
	/** Display bpp */
	public int 		bpp = 		32;
	
	/** Display title */
	public String 	title = 	"Title";
	
	/** Display fullscreen flag 
	 * if ture lunch screen in fullscreen mode 
	 * */
	public boolean 	fullscreen=	false;
	
	/** Display vertical sync flag */
	public boolean 	vsync = 	false;
	
	/** Rendering FPS(Frames per second) limit
	 *  -1 no limit
	 */
	public int 		fpslimit = 	-1;
	
	/** Display x cordinate on desktop */
	public int 		locx = 		-1;
	
	/** Display y cordinate on desktop */
	public int 		locy = 		-1;
	
	/**
	 * Anti-aliasing level 0 means Anti-aliasing disabled
	 */
	public int 		antialiasing = 	0;
	
	/**
	 * Read JRFESettings from a Properties file {@link Properties}
	 * @param resPath Properties File Resrource path 
	 * @return {@link JRFESettings}
	 */
	public static JRFESettings readSettings(String resPath){
		
		Properties p = new Properties();
		try {
			p.load(new BufferedInputStream(
					JRFESettings.class.getClassLoader().getResourceAsStream(resPath)));
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Properties file not found using defaults");
			return new JRFESettings();
		}
		
		JRFESettings settings = new JRFESettings();
		
		String str = p.getProperty("width");
		if(str != null){
			try{
				settings.width = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("width property not a nummber {useing default}");
			}
		}
		str = p.getProperty("height");
		if(str != null){
			try{
				settings.height = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("height property not a nummber {useing default}");
			}
		}
		str = p.getProperty("bpp");
		if(str != null){
			try{
				settings.bpp = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("bpp property not a nummber {useing default}");
			}
		}
		str = p.getProperty("title");
		if(str != null){
				settings.title = str;	
		}
		str = p.getProperty("fullscreen");
		if(str != null){
			try{
				settings.fullscreen = Boolean.parseBoolean(str);
			}
			catch (Exception e) {
				System.err.println("fullscreen property not a Boolean {useing default}");
			}
		}
		str = p.getProperty("vsync");
		if(str != null){
			try{
				settings.vsync = Boolean.parseBoolean(str);
			}
			catch (Exception e) {
				System.err.println("vsync property not a Boolean {useing default}");
			}
		}
		str = p.getProperty("fpslimit");
		if(str != null){
			try{
				settings.fpslimit = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("fpslimit property not a nummber {useing default}");
			}
		}
		str = p.getProperty("locx");
		if(str != null){
			try{
				settings.locx = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("locx property not a nummber {useing default}");
			}
		}
		
		
		str = p.getProperty("locy");
		if(str != null){
			try{
				settings.locy = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("locy property not a nummber {useing default}");
			}
		}
		
		str = p.getProperty("antialiasing");
		if(str != null){
			try{
				settings.antialiasing = Integer.parseInt(str);
			}
			catch (Exception e) {
				System.err.println("antialiasing property not a nummber {useing default}");
			}
		}
		
		return settings;
	}
	
}
	